var pool = require('./pool.js');
var http = require('http');
var url = require('url');
var express = require('express');
var app = express();
var serve = http.createServer(app);
serve.listen(8888);
// post获取值的方法
var bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))

app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});


app.post('/uploading', (req, res) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.json({ code: 1 });
})

app.post('/updata', (req, res) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    var name = req.body.form.name;
    var sex = req.body.form.sex == '男' ? 1 : 0;
    var age = req.body.form.age;
    var tel = req.body.form.tel;
    var address = req.body.form.address;
    var id = req.body.id;
    pool.getConnection((err, conn) => {
        if (err) throw err;
        var sql5 = `UPDATE test_user SET name=?,sex=?,age=?,tel=?,address=? WHERE id=?`;
        conn.query(sql5, [name, sex, age, tel, address, id], (err, result) => {
            if (err) throw err;
            if (result.affectedRows > 0) {
                res.json({ code: 1, msg: '更新成功' })
            } else {
                res.json({ code: -1, msg: '更新失败' })
            }
        })
        conn.release()
    })
})

app.post('/adduser', (req, res) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    var name = req.body.name;
    var sex = req.body.sex == '男' ? 1 : 0;
    var age = req.body.age;
    var tel = req.body.tel;
    var address = req.body.address;
    pool.getConnection((err, conn) => {
        if (err) throw err;
        var sql4 = "INSERT INTO test_user(name,sex,age,tel,address) VALUES(?,?,?,?,?)";
        conn.query(sql4, [name, sex, age, tel, address], (err, result) => {
            if (err) throw err;
            if (result.affectedRows > 0) {
                res.json({ code: 1, msg: '添加成功' })
            } else {
                res.json({ code: -1, msg: '添加失败' })
            }
        })
        conn.release()
    })
})

app.post('/validname', (req, res) => {
    var name = req.body.name;
    res.setHeader('Access-Control-Allow-Origin', '*');
    pool.getConnection((err, conn) => {
        if (err) throw err;
        var sql3 = "SELECT * FROM test_user WHERE name=?";
        conn.query(sql3, [name], (err, result) => {
            if (err) throw err;  
            if (result.length > 0) {
                res.json({ code: 1, msg: '含有此用户名' })
            } else {
                res.json({ code: -1, msg: '不含有此用户名' })
            }
        })
        conn.release()
    })
})

app.get('/count', (req, res) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    pool.getConnection((err, conn) => {
        if (err) throw err;
        var sql2 = "SELECT id FROM test_user";
        conn.query(sql2, (err, result) => {
            if (err) throw err;
            if (result.length > 0) {
                res.json(result.length)
            } else {
                res.json({ code: -1, msg: '删除失败' })
            }
        })
        conn.release()
    })
})

app.post('/remove', (req, res) => {
    var id = parseInt(req.body.id);
    res.setHeader('Access-Control-Allow-Origin', '*');
    pool.getConnection((err, conn) => {
        if (err) throw err;
        var sql = "DELETE FROM test_user WHERE id=?";
        conn.query(sql, [id], (err, result) => {
            if (err) throw err;
            if (result.affectedRows > 0) {
                res.json({ code: 1, msg: '删除成功' })
            } else {
                res.json({ code: -1, msg: '删除失败' })
            }
        })
        conn.release()
    })
})

app.post('/user', (req, res) => {
    var pageIndex = parseInt(req.body.params.pageIndex);
    var pageSize = parseInt(req.body.params.pageSize);
    var pno = (pageIndex - 1) * pageSize;
    pool.getConnection((err, conn) => {
        if (err) throw err;
        var sql = "SELECT * FROM test_user LIMIT ?,?";
        res.setHeader('Access-Control-Allow-Origin', '*');
        conn.query(sql, [pno, pageSize], (err, result) => {
            if (err) throw err;
            if (result.length > 0) {
                res.json(result)
            } else {
                res.json({ code: -1, msg: '你查找的用户不存在' })
            }
        })
        conn.release()
    })
})


app.get('/article', (req, res) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    pool.getConnection((err, conn) => {
        if (err) throw err;
        var art1 = "SELECT * FROM test_article";
        conn.query(art1, (err, result) => {
            if (err) throw err;
            if (result.length > 0) {
                res.json(result)
            } else {
                res.json({ code: -1, msg: '无数据' })
            }
        })
        conn.release()
    })
})
app.post('/articleInfo', (req, res) => {
    var ids = req.body.id;
    res.setHeader('Access-Control-Allow-Origin', '*');
    pool.getConnection((err, conn) => {
        if (err) throw err;
        var art2 = "SELECT * FROM test_article WHERE id=?";
        conn.query(art2, [ids], (err, result) => {
            if (err) throw err;
            if (result.length > 0) {
                res.json(result[0])
            } else {
                res.json({ code: -1, msg: '无数据' })
            }
        })
        conn.release()
    })
})
app.post('/addArticle', (req, res) => {
    let title = req.body.title;
    let author = req.body.author;
    let time = req.body.time;
    let address = req.body.address;
    console.log(title);
    console.log(author);
    console.log(time);
    console.log(address);
    res.json(1);
    // res.setHeader('Access-Control-Allow-Origin', '*');
    // pool.getConnection((err, conn) => {
    //     if (err) throw err;
    //     var art3 = "INSERT INTO test_article(id,title,author,time,address) VALUES(,?,?,?,?)";
    //     conn.query(art3, [title, author, time, address], (err, result) => {
    //         if (err) throw err;
    //         if (result.affectedRows > 0) {
    //             res.json({ code: 1, msg: '添加成功' })
    //         } else {
    //             res.json({ code: -1, msg: '添加失败' })
    //         }
    //     })
    //     conn.release()
    // })
})